package org.example.dynamic_planning;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Longest_increasing_subsequence {
    public static int max = 0;
    public static ArrayList<Integer> arr = new ArrayList<>();

    public static void main(String[] args) {
        //最长递增子序列

        //给你一个整数数组 nums ，找到其中最长严格递增子序列的长度。
        //子序列 是由数组派生而来的序列，删除（或不删除）数组中的元素而不改变其余元素的顺序。例如，[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的
        int[] nums = {10,9,2,5,3,7,101,18};
        int i = lengthOfLIS(nums);
        System.out.println(i);
    }

    //动态规划
    public static int lengthOfLIS(int[] nums) {
        int max = 1;
        int[] dp = new int[nums.length];
        Arrays.fill(dp,1);
        for (int i = 1; i < nums.length; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[j] < nums[i]){
                    dp[i] = Math.max(dp[i],dp[j]+1);
                    max = Math.max(dp[i],max);
                }
            }
        }
        return max;
    }


    //回溯超出时间限制
    public static int lengthOfLIS2(int[] nums) {
        getNumber(nums,0);

        return max;
    }
    public static void getNumber(int[] nums,int deep){
        if (deep == nums.length){
            max = Math.max(max,arr.size());
            return;
        }
        for (int i = deep; i < nums.length; i++) {
            if (deep == 0){
                arr.add(nums[i]);
            }else if (nums[i] > arr.get(arr.size()-1)){
                arr.add(nums[i]);
            }else if (i == nums.length-1 && nums[i] <= arr.get(arr.size()-1)){
                getNumber(nums,i+1);
                continue;
            }else{
                continue;
            }
            getNumber(nums,i+1);
            arr.remove(arr.size()-1);

        }
    }
}
